import FramaAnimations from "./common/FramaAnimations";
import Define from "./common/Define";
import Common from "./common/Common";
import FrameAnimation from "./common/FrameAnimation";
import GameManager from "./manager/GameManager";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class CannonCtr extends cc.Component {

    cannonAnimas:FramaAnimations;
    wheelCount:number = 1;

    nodeCollision:cc.Node;

    rigidBody:cc.RigidBody;
    boxCollider:cc.PhysicsBoxCollider;

    @property(cc.Node)
    node_body: cc.Node = null;
    
    @property(cc.Node)
    node_wheels: cc.Node = null;

    roleAddPowerEffect:FrameAnimation;

    start () {
        this.cannonAnimas = this.node_body.getComponent<FramaAnimations>(FramaAnimations);
        this.cannonAnimas.init()
        this.node.active = true;
        this.wheelCount = this.node_wheels.childrenCount

        this.playNormal();
    }

    openCollision(){
        this.boxCollider = this.node.getComponent<cc.PhysicsBoxCollider>(cc.PhysicsBoxCollider)
        if(this.boxCollider == null){
            this.rigidBody = this.node.addComponent<cc.RigidBody>(cc.RigidBody);
            this.boxCollider = this.node.addComponent<cc.PhysicsBoxCollider>(cc.PhysicsBoxCollider);
            this.rigidBody.type = cc.RigidBodyType.Dynamic
            this.rigidBody.gravityScale = 0
            this.boxCollider.offset = new cc.Vec2(0,-15)
            this.boxCollider.size = new cc.Size(35.4,120);
            this.boxCollider.sensor = true
            this.boxCollider.apply();
        }
    }

    playNormal(){
        this.cannonAnimas.Play(0,50);
        if(this.roleAddPowerEffect != null){
            this.roleAddPowerEffect.node.active = false
        }
    }

    playAttack(){
        this.cannonAnimas.Play(1,20,1,0,cc.callFunc(function(){
            this.cannonAnimas.Play(0,50);
        }.bind(this)));

    }

    shootBullet(isShoot:boolean = false){
        if(this.roleAddPowerEffect != null){
            this.roleAddPowerEffect.node.active = isShoot
        }
    }


    move(dt,isCanMove:boolean,toPost:cc.Vec2){
        if(!isCanMove) return;
        let oldPos = this.node.position;
        //这里执行发射炮弹
        if(Math.abs(oldPos.x - toPost.x) >= 2)
        {
            let distance = oldPos.x - toPost.x;
            let newPos = new cc.Vec2(distance,oldPos.y);
            newPos.normalizeSelf();
            let newX = newPos.x*Define.cannonMoveSpeed*dt;
        
            newPos =  new cc.Vec2(this.node.position.x - newX,this.node.position.y);
            if(newPos.x >= 600){
                newPos.x = 600
            }else if(newPos.x <= 40){
                newPos.x = 40
            }else {
                for (let index = 0; index < this.wheelCount; index++) {
                    this.node_wheels.children[index].rotation = this.node_wheels.children[index].rotation+newX*-1                 
                }
  
            }
            this.node.position = newPos
        }  
    }
    //死亡
    die(callBack:Function){
        Common.actionTinkColor(this.node_body.children[0].children[0],function(){
            callBack();
        }.bind(this))
    }

    addPower(isAdd:boolean){
        if(isAdd){
            this.roleAddPowerEffect = cc.instantiate(Common.addPowerEffect).addComponent<FrameAnimation>(FrameAnimation);
            this.node.addChild(this.roleAddPowerEffect.node)
            this.roleAddPowerEffect.node.position = new cc.Vec2(0,70)
            this.roleAddPowerEffect.init();
            this.roleAddPowerEffect.Play(30)
            this.roleAddPowerEffect.node.active = true
        }else{
            if(this.roleAddPowerEffect != null){
                this.roleAddPowerEffect.node.destroy();
                this.roleAddPowerEffect = null;
            }
        }
    }
}
